define(['angularAMD'], function (angularAMD) {
    angularAMD.service('B0209Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog', '$mddProvider',
        function ($rootScope, $filter, $translate, config, $MMWService, $mdDialog, $mddProvider) {
            ///options : {
            ///    machine_no,
            ///    plotItem,
            ///    feedback
            ///}
            this.init = function (options) {
                var waitInfo= angular.merge({},options.plotItem);
                var nowdatetime = $filter('date')(new Date(), 'yyyy/MM/dd HH:mm:ss');
                waitInfo.equipment_no = options.machine_no;
                waitInfo.working_type = '2';
                waitInfo.rework_type = '0'; //0 表示一般的解除/ 1 表示重工
                function SMES_PlotWaitSetProcess(dialog) {
                    if(dialog.waitInfo.rework_type == '1'){
                        //走重工
                        var rework_options = {
                            parent: 'B0209',
                            machine_no: options.machine_no,
                            plot_detail : [options.plotItem],
                            option:dialog.waitInfo.working_type,
                            feedback: options.feedback,
                            back: function(){
                                openDialog();
                            }
                        };
                        dialog.hide();
                        $mdDialog.serviceDialog('B0213Service', rework_options);
                    } else {
                        //非重工
                        var content = dialog.waitInfo;
                        $MMWService.sendToServer({
                            uri : 'ServicesSTD.Module_WIP.plot_wait_set_process',
                            content : content,
                            success : function(data, response){
                                if(data.MsgLevel==0){
                                    afterProcess(dialog);                             
                                } else if(data.MsgLevel==1){
                                    $rootScope.showConfirm(response.Message, function(confirmDialog){
                                        confirmDialog.hide();
                                        dialog.waitInfo.ignore_validate = 'Y';
                                        SMES_PlotWaitSetProcess(dialog);
                                    });
                                } else if(data.MsgLevel==2){
                                    $rootScope.showAlert(response.Message);
                                }
                            }
                        });
                    }
                }

                function afterProcess(dialog){
                    $rootScope.showToast($filter('translate')('B0209.msg.success'));
                    dialog.hide();
                    if(options.feedback)
                        // 繼續生產
                        if (dialog.waitInfo.working_type == '2' && dialog.waitInfo.rework_type == '0'){
                            var work_time = $filter('date')(nowdatetime.split(' ')[0], 'yyyy/MM/dd') + " " + nowdatetime.split(' ')[1];
                            // 20240325 modify by wenwei for M#160267: [Oracle][H5][设备稼动]生产线工作平台-暂停后设备状态未变
                            options.feedback({
                                machine_no: options.machine_no,
                                report_datetime: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
                                machine_status: '0',
                                remark: '',
                                handle_user_no: config.cache.account,
                                plan_start_time: work_time,
                                actual_end_time: work_time ,
                                remarks: undefined
                            });
                        } else {
                            options.feedback()
                        }
                }

                var openDialog = function (data) {
                    $mddProvider.UseForm('B0209Service', () => {
                        $mdDialog.dialog('module/moduleB/program/B02/B0209/B0209.html',
                            function (dialog) {
                                return {
                                    css_url: 'JSplugins/angular-material-lite/template/dynamicService.tmp.css',
                                    waitInfo: waitInfo,
                                    _event: {
                                        'confirm_type': {
                                            changed: (status) => {
                                                if (status == "0") {//继续生产
                                                    dialog.waitInfo.working_type = "2";
                                                    dialog.waitInfo.rework_type = "0";
                                                } else if (status == "1") {//结束生产
                                                    dialog.waitInfo.working_type = "3";
                                                    dialog.waitInfo.rework_type = "0";
                                                } else if (status == "2") {//整批报废
                                                    dialog.waitInfo.working_type = "4";
                                                    dialog.waitInfo.rework_type = "0";
                                                } else if (status == "3") {//重工
                                                    dialog.waitInfo.working_type = "0";
                                                    dialog.waitInfo.rework_type = "1";
                                                } else if (status == "4") {//让步
                                                    dialog.waitInfo.working_type = "1";
                                                    dialog.waitInfo.rework_type = "1";
                                                } else if (status == "5") {//跳站
                                                    dialog.waitInfo.working_type = "2";
                                                    dialog.waitInfo.rework_type = "1";
                                                } else {//跳流程
                                                    dialog.waitInfo.working_type = "3";
                                                    dialog.waitInfo.rework_type = "1";
                                                }
                                            }
                                        }
                                    },
                                    _fn: {
                                        'Confirm': {
                                            click: () => {
                                                  //20240510 modify by shiwl for 0162442: [6.0.1PATCH][MSSQL][H5][解除暂停]跳站，当前为末道作业站提醒与显示报错
                                                  if( dialog.waitInfo.working_type == '2' && dialog.waitInfo.rework_type == '1' &&  options.next_op_no == $filter('translate')('B0209.label.END')){
                                                    $rootScope.showAlert($filter('translate')('B0213.msg.column15'));
                                                    return ;
                                                }
                                                //20210611 modify by kaiinagami for M#87684 - $scope.plotItem增加next_op_no, 使B0209可以判斷末站
                                                //20210617 modify by kaiinagami for M#87684 - 補上只判斷讓步才卡控
                                                if (dialog.waitInfo.working_type == '1' && dialog.waitInfo.rework_type == '1' &&
                                                    //20210628 modify by kaiinagami for M#87684 - 煩請協助更新讓步程式[龍畿]
                                                    //next_op_no參數獨立
                                                    options.next_op_no == $filter('translate')('B0209.label.END')) {
                                                    $rootScope.showAlert($filter('translate')('B0213.msg.column9') + $filter('translate')('B0213.msg.column10'));
                                                    return;
                                                }

                                                if (dialog.waitInfo.rework_type === '0' && dialog.waitInfo.working_type === '4') {
                                                    // 20200526 modify by WeiTing for M#72218 - 整批報廢增加確認框提示訊息。
                                                    // 是否確定要執行整批報廢？執行後無法還原！
                                                    $rootScope.showConfirm($filter('translate')('B0209.msg.scrap_batch_sure'), function (confirmType) {
                                                        confirmType.hide();
                                                        SMES_PlotWaitSetProcess(dialog);
                                                    });
                                                }
                                                else if (dialog.waitInfo.rework_type === '0' && dialog.waitInfo.working_type === '3') {
                                                    // 20221024 modify by Alan for #109844	 :  回收
                                                    SMES_PlotWaitSetProcess(dialog);
                                                }
                                                else if( data && data.WaitType) {                                       
                                                        if((options.machineItem &&options.machineItem.employee_work_detail.length > 0) || (options.employeeStatus && options.employeeStatus.length > 0))
                                                          {                                                                                    
                                                            SMES_PlotWaitSetProcess(dialog);   
                                                        } else {
                                                               $rootScope.showAlert($filter('translate')('B0206.showAlert.column7'));
                                                        } 
                                                      
                                                        } else {
                                                        SMES_PlotWaitSetProcess(dialog);
                                                 
                                                    }
                                            }
                                        }
                                    }
                                };
                            }
                        );
                    })
                    
                };

                (function SMESPlotWaitInfoGet() {
                    $MMWService.sendToServer({
                        uri : 'ServicesSTD.Module_WIP.plot_wait_info_get',
                        content : {
                            op_no:waitInfo.op_no,
                            plot_no:waitInfo.plot_no,
                            trigger_point: "1", //0 (暫停), 1 (解除暫停)
                            machine_no: waitInfo.equipment_no,
                            status: waitInfo.status
                        },
                        success : function(data){
                            waitInfo.wait_reason_detail=data.wait_reason_detail;
                            waitInfo.qty = data.qty;
                            openDialog(data);
                        }
                    });
                })();

            };
        }]);
});
